package com.jd.oneday.month3;

import java.util.Scanner;

/**
 * 1.链表的创建
 * 2.求链表中的、节点的个数
 * 3.查找单链表中的倒数第个节点
 */
public class Day07 {
    public static void main(String[] args) {

        LinkedListNode3 linkedListNodes1 = new LinkedListNode3(78, "刘伟超", 5);
        LinkedListNode3 linkedListNodes2 = new LinkedListNode3(85, "刘广超", 4);
        LinkedListNode3 linkedListNodes3 = new LinkedListNode3(58, "刘满超", 14);
        LinkedListNode3 linkedListNodes4 = new LinkedListNode3(45, "刘宇鹏", 7);

        ManageLinkedList manageLinkedList = new ManageLinkedList();
        manageLinkedList.add(linkedListNodes1);
        manageLinkedList.add(linkedListNodes2);
        manageLinkedList.add(linkedListNodes3);
        manageLinkedList.add(linkedListNodes4);

        manageLinkedList.list();

        System.out.println("链表的有效个数为" + manageLinkedList.GetEffectiveNumber());

        //请输入需要查找链表中倒数第K个节点
        System.out.println("请输入需要查找链表中倒数第K个节点");
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        manageLinkedList.FindNode(i);
    }

}


class ManageLinkedList {
    private LinkedListNode3 node = new LinkedListNode3(0, "", 0);

    //创建单链表
    public void add(LinkedListNode3 n) {
        LinkedListNode3 temp = node;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = n;
    }


    //链表中有效节点的个数
    public int GetEffectiveNumber() {
        IsNull();
        LinkedListNode3 temp = node.next;
        int number = 0;
        while (true) {
            if (temp == null) {
                break;
            }
            number++;
            temp = temp.next;
        }
        return number;
    }


    //查找链表中倒数第K个节点
    public void FindNode(int n) {
        IsNull();
        LinkedListNode3 temp = node.next;
        boolean flag = false;
        int size = GetEffectiveNumber();

        if (n > size || n < 0) {
            System.out.println("没有这个节点");
            return;
        }

        for (int i = 0; i <= size - n; i++) {   //size-n  得到链表中到数第K个节点
            if (i == size - n) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.println(temp);
        } else {
            System.out.println("没有这个节点");
        }
    }


    //遍历链表
    public void list() {
        IsNull();

        LinkedListNode3 temp = node;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
            System.out.println(temp);
        }
    }

    //判断链表是否为空
    public void IsNull() {
        if (node.next == null) {
            System.out.println("这是一个空链表");
            return;
        }
    }
}


class LinkedListNode3 {

    int id;
    String name;
    int age;
    LinkedListNode3 next;

    public LinkedListNode3(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "LinkedListNode3{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
